Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement std's thread parker on top of events on SGX #98391

Merged
merged 3 commits into from
Dec 10, 2022

Conversation

joboet
Copy link
Contributor

@joboet joboet commented Jun 22, 2022

Mutex and Condvar are being replaced by more efficient implementations, which need thread parking themselves (see #93740). Therefore, the generic Parker needs to be replaced on all platforms where the new lock implementation will be used.

SGX enclaves have a per-thread event state, which allows waiting for and setting specific bits. This is already used by the current mutex implementation. The thread parker can however be much more efficient, as it only needs to store the TCS address of one thread. This address is stored in a state variable, which can also be set to indicate the thread was already notified.

park_timeout does not guard against spurious wakeups like the current condition variable does. This is allowed by the API of Parker, and I think it is better to let users handle these wakeups themselves as the guarding is quite expensive and might not be necessary.

@jethrogb as you wrote the initial SGX support for std, I assume you are the target maintainer? Could you help me test this, please? Lacking a x86_64 chip, I can't run SGX.

@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 22, 2022
@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 22, 2022
@jethrogb
Copy link
Contributor

Cool stuff. It will take use some time to review this to ensure security. cc @mzohreva @raoulstrackx

@Mark-Simulacrum
Copy link
Member

Once you have reviewed, please re-assign back to me or someone else for the final r+

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 24, 2022
@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

bors bot added a commit to fortanix/rust-sgx that referenced this pull request Sep 5, 2022
408: Document `TCS` struct limitations r=jethrogb a=raoulstrackx

Document that a TCS struct should never be located as the beginning of an enclave. (ref rust-lang/rust#98391)

409: Fix broken link to std library r=jethrogb a=raoulstrackx



Co-authored-by: Raoul Strackx <raoul.strackx@fortanix.com>
bors bot added a commit to fortanix/rust-sgx that referenced this pull request Sep 5, 2022
408: Document `TCS` struct limitations r=jethrogb a=raoulstrackx

Document that a TCS struct should never be located as the beginning of an enclave. (ref rust-lang/rust#98391)

Co-authored-by: Raoul Strackx <raoul.strackx@fortanix.com>
@raoulstrackx
Copy link
Contributor

bors r+

@joboet
Copy link
Contributor Author

joboet commented Oct 12, 2022

Thank you for the review!

r? @Mark-Simulacrum

@Mark-Simulacrum
Copy link
Member

r? @m-ou-se

@m-ou-se
Copy link
Member

m-ou-se commented Dec 8, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Dec 8, 2022

📌 Commit a40d300 has been approved by m-ou-se

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 8, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 10, 2022
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#98391 (Reimplement std's thread parker on top of events on SGX)
 - rust-lang#104019 (Compute generator sizes with `-Zprint_type_sizes`)
 - rust-lang#104512 (Set `download-ci-llvm = "if-available"` by default when `channel = dev`)
 - rust-lang#104901 (Implement masking in FileType comparison on Unix)
 - rust-lang#105082 (Fix Async Generator ABI)
 - rust-lang#105109 (Add LLVM KCFI support to the Rust compiler)
 - rust-lang#105505 (Don't warn about unused parens when they are used by yeet expr)
 - rust-lang#105514 (Introduce `Span::is_visible`)
 - rust-lang#105516 (Update cargo)
 - rust-lang#105522 (Remove wrong note for short circuiting operators)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ae8794c into rust-lang:master Dec 10, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 10, 2022
@joboet joboet deleted the sgx_parker branch December 22, 2022 08:58
Manishearth added a commit to Manishearth/rust that referenced this pull request May 3, 2023
…ou-se

Replace generic thread parker with explicit no-op parker

With rust-lang#98391 merged, all platforms supporting threads now have their own parking implementations. Therefore, the generic implementation can be removed. On the remaining platforms (really just WASM without atomics), parking is not supported, so calls to `thread::park` now return instantly, which is [allowed by their API](https://doc.rust-lang.org/nightly/std/thread/fn.park.html). This is a change in behaviour, as spurious wakeups do not currently occur since all platforms guard against them. It is invalid to depend on this, but I'm still going to tag this as libs-api for confirmation.

`@rustbot` label +T-libs +T-libs-api +A-atomic

r? rust-lang/libs
Manishearth added a commit to Manishearth/rust that referenced this pull request May 3, 2023
…ou-se

Replace generic thread parker with explicit no-op parker

With rust-lang#98391 merged, all platforms supporting threads now have their own parking implementations. Therefore, the generic implementation can be removed. On the remaining platforms (really just WASM without atomics), parking is not supported, so calls to `thread::park` now return instantly, which is [allowed by their API](https://doc.rust-lang.org/nightly/std/thread/fn.park.html). This is a change in behaviour, as spurious wakeups do not currently occur since all platforms guard against them. It is invalid to depend on this, but I'm still going to tag this as libs-api for confirmation.

``@rustbot`` label +T-libs +T-libs-api +A-atomic

r? rust-lang/libs
Manishearth added a commit to Manishearth/rust that referenced this pull request May 3, 2023
…ou-se

Replace generic thread parker with explicit no-op parker

With rust-lang#98391 merged, all platforms supporting threads now have their own parking implementations. Therefore, the generic implementation can be removed. On the remaining platforms (really just WASM without atomics), parking is not supported, so calls to `thread::park` now return instantly, which is [allowed by their API](https://doc.rust-lang.org/nightly/std/thread/fn.park.html). This is a change in behaviour, as spurious wakeups do not currently occur since all platforms guard against them. It is invalid to depend on this, but I'm still going to tag this as libs-api for confirmation.

```@rustbot``` label +T-libs +T-libs-api +A-atomic

r? rust-lang/libs
Manishearth added a commit to Manishearth/rust that referenced this pull request May 3, 2023
…ou-se

Replace generic thread parker with explicit no-op parker

With rust-lang#98391 merged, all platforms supporting threads now have their own parking implementations. Therefore, the generic implementation can be removed. On the remaining platforms (really just WASM without atomics), parking is not supported, so calls to `thread::park` now return instantly, which is [allowed by their API](https://doc.rust-lang.org/nightly/std/thread/fn.park.html). This is a change in behaviour, as spurious wakeups do not currently occur since all platforms guard against them. It is invalid to depend on this, but I'm still going to tag this as libs-api for confirmation.

````@rustbot```` label +T-libs +T-libs-api +A-atomic

r? rust-lang/libs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants